package com.xunshibao.tutor.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xunshibao.core.page.Pagination;
import com.xunshibao.core.security.Security;
import com.xunshibao.core.web.IpAddress;
import com.xunshibao.core.web.SessionUser;
import com.xunshibao.spring.validator.ValidationTools;
import com.xunshibao.tutor.concern.ConcernServiceImpl;
import com.xunshibao.tutor.po.TutorInfo;
import com.xunshibao.tutor.user.service.TutorInfoServiceImpl;

@Controller
@RequestMapping(value="/tutorinfo/*")
public class TutorInfoController {
	@Autowired
	private ValidationTools validator;
	@Autowired
	private TutorInfoServiceImpl infoService;
	
	@Autowired
	private ConcernServiceImpl concernService;
	
	@RequestMapping(value = "edit", method=RequestMethod.GET)
    public String editTutorInfo() {
        return "tutor/info";
    }
	
	@RequestMapping(value = "save", method=RequestMethod.POST)
    public @ResponseBody Map<String, Object> saveTutorInfo(TutorInfo tutorInfo, BindingResult result, IpAddress ip) {
		validator.validate("/tutorinfo/save", tutorInfo, result);
	    Map<String, Object> json = new HashMap<String, Object>();
		if(result.hasErrors()) {
		    json.put("status", 0); 
		} else {
		    int ret = infoService.save(tutorInfo, ip.value(), 1l);//TODO ip和用户编号替换成变量
		    json.put("status", ret);
		}
		
        return json;
    }
	
	@RequestMapping(value = "list", method=RequestMethod.POST)
    public @ResponseBody Pagination list(@RequestParam(value="pn", defaultValue="1") Integer pageNo, @RequestParam(value="s", defaultValue="10")Integer pageSize) {
		Pagination page = infoService.list(pageNo, pageSize);
		return page;
	}
	
	@RequestMapping(value = "list", method=RequestMethod.GET)
	@Security
    public String list() {
        return "tutor/infolist";
    }
	
	@RequestMapping(value = "detail/{id}", method=RequestMethod.GET)
    public String info(@PathVariable(value="id") Long id, Model model) {
		model.addAttribute("id", id);
        return "tutor/detail";
    }
	
	@RequestMapping(value = "detail/{id}", method=RequestMethod.POST)
    public @ResponseBody Map<String, Object> jsonInfo(@PathVariable(value="id") Long id, SessionUser user) {
		Map<String, Object> json = new HashMap<String, Object>();
		if(user != null) {
			boolean concerned = concernService.isConcerned(id, user.getUserId());
			if(concerned) {
				json.put("concerned", 1); //关注过
			} else {
				json.put("concerned", 0);//没有关注过
			}
		} else {
			json.put("concerned", 2);//需要登录
		}
		TutorInfo info= infoService.view(id);
		json.put("info", info);
		return json;
    }
}
